package com.jd.order.service.impl;

import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.basic.util.LoginContext;
import com.jd.basic.util.PageList;
import com.jd.order.domain.OrderPetAcquisition;
import com.jd.order.dto.OrderDto;
import com.jd.order.mapper.OrderPetAcquisitionMapper;
import com.jd.order.query.OrderPetAcquisitionQuery;
import com.jd.order.service.IOrderPetAcquisitionService;
import com.jd.org.domain.Employee;
import com.jd.org.domain.Shop;
import com.jd.org.mapper.EmpMapper;
import com.jd.org.mapper.ShopMapper;
import com.jd.pet.domain.Pet;
import com.jd.pet.domain.SearchMasterMsg;
import com.jd.pet.mapper.PetMapper;
import com.jd.pet.mapper.SearchMasterMsgMapper;
import com.jd.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    @Transactional
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        // 通过获取登录人信息，获取员工对象
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = empMapper.loadByLogininfoId(logininfo.getId());
        //1.判断登录人身份
        // 没有shopid为平台管理员，有为店铺管理员
        Long shopId = employee.getShop_id();
        if (shopId!=null){//店铺管理员
            Shop shop = shopMapper.loadById(shopId);
            //店铺管理员分为店长和店员
            if (!shop.getAdminId().equals(employee.getId())){//通过店铺的管理员id和员工的id比较
                //管理员id和员工表id相等的就是店长,否之为店员
                query.setEmpId(employee.getId());
            }
            //shopid不为null，就是店铺管理员
            query.setShopId(shopId);
        }
        return super.queryPage(query);
    }

    @Override
    @Transactional
    public void confirm(OrderDto orderDto) {
        //1.接收前端传入的三个参数，修改订单状态0待处理 - 1已处理
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(orderDto.getId());
        order.setPaytype(orderDto.getPayType());
        order.setPrice(orderDto.getMoney());
        order.setState(1);
        orderPetAcquisitionMapper.update(order);
        //2.修改寻主信息的状态1待处理 -2已完成
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        msg.setState(2);
        searchMasterMsgMapper.update(msg);
        //修改宠物的价格
        Pet pet = petMapper.loadById(order.getPetId());
        pet.setCostprice(orderDto.getMoney());
        petMapper.update(pet);
    }

    @Override
    @Transactional
    public void cancel(Long id) {
        // 通过订单id，获取订单信息
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        //1.置空shopid
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());
        //2.删除宠物信息，点击取消订单
        petMapper.remove(order.getPetId());
        //3.修改状态
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
    }
}
